package com.xbongbong.parent.condition.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FilterConditionsPoJo;
import com.xbongbong.paas.field.LinkConditionPoJo;
import com.xbongbong.paas.field.LinkInfoPojo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.parent.abstracts.AbstractConditionFieldHandler;
import com.xbongbong.pro.businessrule.pojo.BusinessRuleInfoPojo;
import com.xbongbong.pro.condition.pojo.ConditionDTO;
import com.xbongbong.pro.user.pojo.ReturnUserAndDepartmentPojo;
import com.xbongbong.saas.util.UserFieldUtil;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 成员和部门处理
 * 注：此处主要是处理关联数据和数据联动多条件，业务规则的不会进
 * @author: xingxing.xiao
 * @date: 2020/08/04 16:00
 */
@Component
public class ConditionFormUserAndDeptHandlerImpl extends AbstractConditionFieldHandler {


    @Override
    public List<Integer> fieldType() {
        return Arrays.asList(FieldTypeEnum.USER.getType(), FieldTypeEnum.DEPT.getType(), FieldTypeEnum.USER_GROUP.getType(), FieldTypeEnum.DEPT_GROUP.getType());
    }

    @Override
    public Object handlerFixedValue(FieldAttrEntity targetField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterCondition) throws XbbException {
        if (filterCondition.getValue() instanceof Map) {
            // 成员单选和部门单选
            JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(filterCondition.getValue()));
            return jsonObject.get("id");
        }
        // 若放开了成员多选和部门多选，此处还要再做处理
        return filterCondition.getValue();
    }

    @Override
    public Object handlerPreConditionValue(FieldAttrEntity thisField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterCondition) throws XbbException {
        return UserFieldUtil.getUserIdAndDeptIdList(filterCondition.getValue(), filterCondition.getSymbol());
    }

    @Override
    public Object handlerRelyFormField(String corpid, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, LinkConditionPoJo linkConditionPoJo, LinkInfoPojo linkInfoPojo, Object value) throws XbbException {
        if (value instanceof List) {
            List<ReturnUserAndDepartmentPojo> list = JSON.parseArray(JSON.toJSONString(value), ReturnUserAndDepartmentPojo.class);
            if (!list.isEmpty()) {
                List<Object> valueList = new ArrayList<>();
                list.forEach(item -> valueList.add(item.getId()));
                value = valueList;
            }
        } else if (value instanceof Map) {
            // Map<String, Object> map = (Map<String, Object>) value;
            // value = map.getOrDefault("id", "");
            ReturnUserAndDepartmentPojo pojo = JSONObject.parseObject(JSON.toJSONString(value), ReturnUserAndDepartmentPojo.class);
            if (Objects.nonNull(pojo)) {
                value = pojo.getId();
            }
        }
        return value;
    }

    @Override
    public Object handlerLinkDataFixedValue(String corpid, FieldAttrEntity targetField, LinkInfoPojo linkInfoPojo, Object value) throws XbbException {
        if (value instanceof Map) {
            // 成员单选和部门单选
            JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(value));
            return jsonObject.get("id");
        } else if (value instanceof List) {
            // 成员多选和部门多选
            List<ReturnUserAndDepartmentPojo> list = JSON.parseArray(JSON.toJSONString(value), ReturnUserAndDepartmentPojo.class);
            if (!list.isEmpty()) {
                List<Object> valueList = new ArrayList<>();
                list.forEach(item -> valueList.add(item.getId()));
                value = valueList;
            }
        }
        return value;
    }

    @Override
    public Object handlerBusinessRelyFormField(String corpid, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, LinkConditionPoJo linkConditionPoJo, Object value, Long linkFormId, Integer linkBusinessType, Integer linkSaasMark) throws XbbException {
        if (value instanceof List) {
            List<ReturnUserAndDepartmentPojo> list = JSON.parseArray(JSON.toJSONString(value), ReturnUserAndDepartmentPojo.class);
            if (!list.isEmpty()) {
                List<Object> valueList = new ArrayList<>();
                list.forEach(item -> valueList.add(item.getId()));
                value = valueList;
            }
        } else if (value instanceof Map) {
            ReturnUserAndDepartmentPojo pojo = JSONObject.parseObject(JSON.toJSONString(value), ReturnUserAndDepartmentPojo.class);
            if (Objects.nonNull(pojo)) {
                value = pojo.getId();
            }
        }
        return value;
    }

    @Override
    public Object handlerBusinessDataFixedValue(String corpid, FieldAttrEntity targetField, ConditionDTO conditionDTO, Object value) throws XbbException {
        if (value instanceof Map) {
            // 成员单选和部门单选
            JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(value));
            return jsonObject.get("id");
        }
        return value;
    }
}
